//
//  CardScrollView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/31.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol CardScrollViewDelegate {
    
    @objc optional func showNewsDetails(model:RecommendModel) //显示推荐详情
    @objc optional func showRecommendDetails(id:String,urlStr:String,title:String) //显示申请详情
    @objc optional func selectType(title:String) //选择服务类型
    @objc optional func selectBankType(title:String,image:String) //选择银行类型
    @objc optional func selectCardType(title:String) //选择卡片类型
    @objc optional func showNotice() //显示公告
    @objc optional func showStrategyDetails(model: NewsModel) //显示攻略详情
    @objc optional func selectStrategyLabel(index:Int) //选择攻略标签
    @objc optional func selectCardBanner(url:String) //选择轮播图
}

class CardScrollView: UIView {
    
    weak var cardDelegate : CardScrollViewDelegate?

    let scrollView = UIScrollView()
    let cardBannerView = CardBannerView() //轮播图
    let headView = CardHeadView() //服务
    let bankView = BankView() //银行
    let themeView = ThemeView() //主题
    let recommendView = RecommendView() //网友推荐
    let strategyView = StrategyView() //攻略
    var noticeView : NoticeHeadView! //公告
    
    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.mineColor()

        scrollView.showsVerticalScrollIndicator = false
        self.addSubview(scrollView)

        cardBannerView.delegate = self
        headView.cardDelegate = self
        bankView.bankDelegate = self
        themeView.themeDelegate = self
        recommendView.recommendDelegate = self
        strategyView.delegate = self

        noticeView = NoticeHeadView.init(frame: .init(x: 0, y: 0, width: ScreenW, height: RATIO_H(maxNum: 40)))
        noticeView.addTarget(self, action: #selector(showNotice), for: .touchUpInside)

        scrollView.addSubview(cardBannerView)
        scrollView.addSubview(noticeView)
        scrollView.addSubview(headView)
        scrollView.addSubview(bankView)
        scrollView.addSubview(themeView)
        scrollView.addSubview(recommendView)
        scrollView.addSubview(strategyView)

        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
        
        cardBannerView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalTo(self)
            make.height.equalTo(RATIO_H(maxNum: 150))
        }
        
        noticeView.snp.makeConstraints { (make) in
            make.top.equalTo(cardBannerView.snp.bottom)
            make.left.right.equalTo(self)
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        themeView.snp.makeConstraints { (make) in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(noticeView.snp.bottom)
            make.height.equalTo(RATIO_H(maxNum: 180))
        }

        bankView.snp.makeConstraints { (make) in
            make.left.equalTo(self)
            make.right.equalTo(self)
        make.top.equalTo(themeView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.height.equalTo(RATIO_H(maxNum: 242))
        }

        headView.snp.makeConstraints { (make) in
            make.left.equalTo(self)
            make.right.equalTo(self)
        make.top.equalTo(bankView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.height.equalTo(RATIO_H(maxNum: 100))
        }

        recommendView.snp.makeConstraints { (make) in
            make.left.equalTo(self)
            make.right.equalTo(self)
        make.top.equalTo(headView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.height.equalTo(RATIO_H(maxNum: 42))
        }
        
        strategyView.snp.makeConstraints { (make) in
            make.left.equalTo(self)
            make.right.equalTo(self)
        make.top.equalTo(recommendView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.height.equalTo(RATIO_H(maxNum: 144))
        }

        self.layoutIfNeeded()

        scrollView.showsVerticalScrollIndicator = false
        scrollView.contentSize = CGSize.init(width: ScreenW, height: strategyView.frame.maxY + 10)

        KMTool.setRefresh(with: scrollView, headerAction: {[weak self] in

            self?.recommendView.getData()
            self?.strategyView.getData()

        }, footerAction: nil)
        
    }
    
    //MARK: 显示公告
    @objc fileprivate func showNotice() {
        
        self.cardDelegate?.showNotice!()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension CardScrollView : RecommendDelegate,CardHeadViewDelegate,BankViewDelegate,ThemeViewDelegate,StrategyViewDelegate,CardBannerViewDelegate {
    
    //MARK: 选择轮播图
    func selectCardBanner(url: String) {
        self.cardDelegate?.selectCardBanner!(url: url)
    }
    
    //MARK: 显示信息详情
    func showNewsDetails(model:RecommendModel) {
        
        self.cardDelegate?.showNewsDetails!(model: model)
    }
    
    //MARK: 免费申请详情
    func showRecommendDetails(id: String,urlStr: String,title:String) {
        
        self.cardDelegate?.showRecommendDetails!(id: id, urlStr: urlStr, title: title)
    }
    
    //MARK: 选择服务类型
    func selectType(title: String) {
        
        self.cardDelegate?.selectType!(title: title)
    }
    
    //MARK: 选择银行类型
    func selectBankType(title: String,image: String) {
        
        self.cardDelegate?.selectBankType!(title: title, image: image)
    }
    
    //MARK: 选择卡片类型
    func selectCardType(title: String) {
        
        self.cardDelegate?.selectCardType!(title: title)
    }
    
    //MARK: 显示攻略详情
    func showStrategyDetails(model: NewsModel) {
        
        self.cardDelegate?.showStrategyDetails!(model: model)
    }
    
    //MARK: 选择攻略标签
    func selectStrategyLabel(index: Int) {
        self.cardDelegate?.selectStrategyLabel!(index: index)
    }
    
    //MARK: 改变tableView坐标
    func changeTableViewFrame(count: Int) {
        
        if self.scrollView.mj_header != nil {
            self.scrollView.mj_header.endRefreshing()
        }
        
        let height = RATIO(maxNum: 42) + CGFloat(count) * RATIO_H(maxNum: 90)
        
        recommendView.snp.remakeConstraints { (make) in
            make.left.equalTo(self)
            make.right.equalTo(self)
        make.top.equalTo(headView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.height.equalTo(height)
        }
        
        self.layoutIfNeeded()
        
        scrollView.contentSize = CGSize.init(width: ScreenW, height: strategyView.frame.maxY + 10)
    }
    
    //MARK: 修改攻略坐标
    func changeStrategyViewFrame(count: Int) {
        
        if self.scrollView.mj_header != nil {
            self.scrollView.mj_header.endRefreshing()
        }
        
        let height = RATIO(maxNum: 144) + CGFloat(count) * RATIO_H(maxNum: 100)
        
        strategyView.snp.remakeConstraints { (make) in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(recommendView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.height.equalTo(height)
        }
        
        self.layoutIfNeeded()
        
        scrollView.contentSize = CGSize.init(width: ScreenW, height: strategyView.frame.maxY + 10)
    }
    
    //MARK: 停止刷新
    func stopRefresh() {
        
        if self.scrollView.mj_header != nil {
            self.scrollView.mj_header.endRefreshing()
        }
    }
    
    //MARK: 攻略停止刷新
    func stopStrategyRefresh() {
        
        if self.scrollView.mj_header != nil {
            self.scrollView.mj_header.endRefreshing()
        }
        
    }
}
